@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">出入库记录</h5>
                    @if(Auth::user()->hasRole('admin'))
                    <div>
                        <a href="{{ route('inventory.create-in') }}" class="btn btn-success btn-sm">
                            <i class="bi bi-plus-circle"></i> 入库
                        </a>
                        <a href="{{ route('inventory.create-out') }}" class="btn btn-warning btn-sm">
                            <i class="bi bi-dash-circle"></i> 出库
                        </a>
                        <a href="{{ route('inventory.export') }}" class="btn btn-info btn-sm">
                            <i class="bi bi-download"></i> 导出
                        </a>
                    </div>
                    @endif
                </div>
                <div class="card-body">
                    @if (session('success'))
                        <div class="alert alert-success">
                            {{ session('success') }}
                        </div>
                    @endif

                    <form action="{{ route('inventory.index') }}" method="GET" class="mb-4">
                        <div class="row g-3">
                            <div class="col-md-3">
                                <label for="material_id" class="form-label">材料</label>
                                <select name="material_id" id="material_id" class="form-select">
                                    <option value="">全部材料</option>
                                    @foreach($materials as $material)
                                        <option value="{{ $material->id }}" 
                                            {{ request('material_id') == $material->id ? 'selected' : '' }}>
                                            {{ $material->name }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-3">
                                <label for="type" class="form-label">类型</label>
                                <select name="type" id="type" class="form-select">
                                    <option value="">全部类型</option>
                                    <option value="1" {{ request('type') == '1' ? 'selected' : '' }}>入库</option>
                                    <option value="2" {{ request('type') == '2' ? 'selected' : '' }}>出库</option>
                                </select>
                            </div>
                            <div class="col-md-3">
                                <label for="user_id" class="form-label">操作人</label>
                                <select name="user_id" id="user_id" class="form-select">
                                    <option value="">全部操作人</option>
                                    @foreach($users as $user)
                                        <option value="{{ $user->id }}" 
                                            {{ request('user_id') == $user->id ? 'selected' : '' }}>
                                            {{ $user->username }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-3">
                                <label for="date_start" class="form-label">开始日期</label>
                                <input type="date" class="form-control" id="date_start" name="date_start" 
                                    value="{{ request('date_start') }}">
                            </div>
                            <div class="col-md-3">
                                <label for="date_end" class="form-label">结束日期</label>
                                <input type="date" class="form-control" id="date_end" name="date_end" 
                                    value="{{ request('date_end') }}">
                            </div>
                            <div class="col-md-3 d-flex align-items-end">
                                <button type="submit" class="btn btn-primary me-2">搜索</button>
                                <a href="{{ route('inventory.index') }}" class="btn btn-secondary">重置</a>
                            </div>
                        </div>
                    </form>

                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>ID</th>
                                    <th>时间</th>
                                    <th>材料</th>
                                    <th>类型</th>
                                    <th>数量</th>
                                    <th>操作人</th>
                                    <th>备注</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($records as $record)
                                    <tr class="{{ $loop->even ? 'table-light' : '' }}">
                                        <td>{{ $record->id }}</td>
                                        <td>{{ $record->created_at->format('Y-m-d H:i:s') }}</td>
                                        <td>{{ $record->material->name }}</td>
                                        <td>
                                            <span class="badge bg-{{ $record->type == 1 ? 'success' : 'warning' }}">
                                                {{ $record->type == 1 ? '入库' : '出库' }}
                                            </span>
                                        </td>
                                        <td>{{ $record->quantity }} {{ $record->material->unit }}</td>
                                        <td>{{ $record->username ?? '已删除用户' }}</td>
                                        <td>{{ $record->remark }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <div class="d-flex justify-content-center mt-4">
                        {{ $records->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection 